#!usr/bin/env python  
# -*- coding:utf-8 _*-
""" 
@author:robot
@file: some_useful.py 
@version:
@time: 2022/03/11 
"""
import time
from functools import wraps

from TapUtil.logfunc.logger import logger


def more_time_check(assert_thing, outer_time, interval=5):
    def middle(func):
        def check(*args, **kwargs):
            for _ in range(outer_time):
                result = func(*args, **kwargs)
                logger.info(result)
                if result == assert_thing:
                    logger.info(result)
                else:
                    logger.fatal(result)
                    raise f'assert_thing not equals {assert_thing}'
                time.sleep(interval)

        return check

    return middle


def timeout(assert_thing, outer, interval=5):
    """
    装饰函数返回判定
    :param assert_thing: 函数返回值
    :param outer: 等待次数
    :param interval: 等待间隔时间s
    :return:
    """

    def middle(func):
        def check(*args, **kwargs):
            for _ in range(outer):
                result = func(*args, **kwargs)
                logger.info(result)
                if result:
                    if result in assert_thing:
                        return result
                    if result == 'error':
                        logger.fatal('result is error')
                        raise Exception
                time.sleep(interval)
            logger.fatal('等待超时')
            raise Exception

        return check

    return middle


def func_logger(robot):
    """
    语句执行装饰圈日志
    :param robot:
    :return:
    """

    @wraps(robot)
    def conLog(*args, **kwargs):
        if args:
            logger.info(f'args >>> {args}')
        if kwargs:
            logger.info(f'args >>> {kwargs}')
        result = robot(*args, **kwargs)
        logger.info(f'return is >>> {result}')
        return result

    return conLog
